/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.milib.dbg.view.content {
	import com.googlecode.milib.base.IMLBroadcasterListener;
	import com.googlecode.milib.data.ScrollableArray;
	import com.googlecode.milib.dbg.model.content.MLDBGValuesListModel;
	import com.googlecode.milib.dbg.model.content.MLDBGValuesListModelItem;
	import com.googlecode.milib.dbg.view.content.AbstractMLDBGWindowContent;
	import com.googlecode.milib.display.ui.list.MCList;
	import com.googlecode.milib.display.ui.list.MCListAbstractElement;
	import com.googlecode.milib.display.ui.list.MCListOwner;
	import com.googlecode.milib.util.MLArrayUtil;
	import com.googlecode.milib.vo.MLEventInfo;

	import flash.display.MovieClip;
	import flash.utils.getDefinitionByName;

	/**
	 * @author Marek Brun
	 */
	public class MLDBGValuesListView extends AbstractMLDBGWindowContent implements MCListOwner, IMLBroadcasterListener {

		private var list:MCList;
		private var data:ScrollableArray;
		private var model:MLDBGValuesListModel;
		
		public function MLDBGValuesListView(name:String) {
			var MCListClass:Class=Class(getDefinitionByName('MCMLDBGWindowContentList'));
			super(new MCListClass(), name);
			list=new MCList(mlmc.mc, this);
			
			data=new ScrollableArray();
			list.setModel(data);
		}
		
		override protected function drawData():void {
			 model.refresh();
		}
		
		override protected function doSetupScroller():void {
			scroller.setScrolled(data);
		}
		
		public function createMCListElement(mc:MovieClip, index:uint):MCListAbstractElement {
			var element:MLDBGValuesListViewElement=new MLDBGValuesListViewElement(mc);
			addEventsMapping(element);
			return element;
		}
		
		override protected function doSize():void {
			list.setHeight(height);			list.setWidth(width);
		}
		
		override protected function doIsEnabled():void {
			scroller.setScrolled(data);
		}
		
		override protected function doIsDisabled():void {
			//this.model.removeListener(this);
			scroller.unsetScrolled();
		}
		
		public function setModel(model:MLDBGValuesListModel):void {
			this.model=model;
			this.model.addListener(this);
			var arr:Array=model.toArray();
			var i:uint;
			var loop:*;
			for(i=0;i<arr.length;i++){
				loop=arr[i];
			}
			data.setNewArray(model.toArray());
		}
		
//********************************************************************************************
//		events for MLDBGValuesList
//********************************************************************************************
		public function onEvent(ev:MLEventInfo):void {
			switch(ev.hero){
				case model:
					switch(ev.event) {
						case model.event_ItemAdded:
							data.push(ev.data);
							setTitle(MLDBGValuesListModelItem(ev.data).getName()+' : '+MLDBGValuesListModelItem(ev.data).getValue());
						break;
						case model.event_ItemChanged:
							setTitle(MLDBGValuesListModelItem(ev.data).getName()+' : '+MLDBGValuesListModelItem(ev.data).getValue());
						break;
						case model.event_ItemRemoved:
							var newArr:Array=data.getBaseArray();
							MLArrayUtil.remove(newArr, ev.data);
							data.setNewArray(newArr);
						break;
					}
				break;
			}
		}
		
	}
}
